package com.qf.shopping.servlet.frontController;

import com.qf.shopping.common.Constants;
import com.qf.shopping.common.DisatpcherServlet;
import com.qf.shopping.entity.Product;
import com.qf.shopping.entity.ProductType;
import com.qf.shopping.service.IProductService;
import com.qf.shopping.service.IProductTypeService;
import com.qf.shopping.service.impl.ProductServiceImpl;
import com.qf.shopping.service.impl.ProductTypeServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**处理前端页面商品的初始化
 * @author 三旬
 * @version 1.0  2022/9/20
 */
@WebServlet("/IndexController/*")
public class IndexController extends DisatpcherServlet {
    //创建访问service层的对象接口
    private IProductService product = new ProductServiceImpl();//访问商品
    private IProductTypeService productType = new ProductTypeServiceImpl();//访问商品类型

    /**
     * 在首页展示商品的信息
     * @param request
     * @param response
     */
    public String index(HttpServletRequest request, HttpServletResponse response){
        //查询所有商品上架商品
        List<Product> products = product.selectProductByStatu();
        //查询所有的商品类型
        List<ProductType> productTypes = productType.selectAll();
        //将数据放到作用域中
        request.setAttribute("giList",products);
        request.setAttribute("gtList",productTypes);
        System.out.println(products);
        System.out.println(productTypes);
        //转跳到首页
        return Constants.FORWARD+":index.jsp";

    }

    /**
     * 展示商品的信息
     * @param request
     * @param response
     * @return
     */
    public String info(HttpServletRequest request,HttpServletResponse response){
        //接收商品的id
        Integer id = Integer.valueOf(request.getParameter("id"));
        //调用业务层查询商品
        Product product = this.product.selectOneById(id);
        //根据结果响应
        if (product  != null) {
            request.setAttribute("product",product);
            return Constants.FORWARD+":introduction.jsp";
        }
        return Constants.SCRIPT+":"+"商品不存在";
    }



}
